<?php
declare(strict_types=1);

namespace App\Logic;

use App\Event\AdministratorLogsEvent;
use App\Models\Account as AccountModel;
use Illuminate\Support\Facades\Request;

class Login
{
    public function login(string $account, string $password): bool
    {
        $password = md5('fengadmin&' . $password);
        $model = AccountModel::where([['account', '=', $account], ['password', '=', $password], ['account_status', '=', 1]])->first();

        if (empty($model)) {
            return false;
        } else {
            $model->login_num += 1;
            $model->login_time = time();
            $model->login_ip = Request::ip();
            $model->save();

            Request::session()->put('account', $model->account);
            Request::session()->put('account_id', $model->id);
            Request::session()->put('account_name', $model->account_name);
            event(new AdministratorLogsEvent("登录内容管理系统"));
            return true;
        }
    }
}
